package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.widget.SearchView;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseFragment;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.entity.BaseParams;
import com.isunland.managesystem.entity.FunctionObject;
import com.isunland.managesystem.entity.ZTreeNode;
import com.isunland.managesystem.utils.ConfigUtil;
import com.isunland.managesystem.utils.MyStringUtil;
import com.isunland.managesystem.utils.SharedPreferencesUtil;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * 对象功能:
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: wangyanan
 * 创建时间: 2017/6/3 23:37
 */
public class FunctionSearchListFragment extends BaseListFragment {
  private static final int MENU_ITEM_CLEAR = 1;

  private ArrayAdapter<FunctionObject> mAdapter;
  private ArrayList<FunctionObject> mList;
  private ArrayList<FunctionObject> mListOriginal;

  @Override public String getUrl() {
    return null;
  }

  @Override public void resolveResponse(String json) {

  }

  @Override public void initData() {
    super.initData();
    setIsForbiddenVolley(true);//禁用volley
    HashMap<Integer, FunctionObject> mMap = ConfigUtil.newInstance(mActivity).getMap();
    mList = new ArrayList<>();
    mListOriginal = new ArrayList<>(mMap.values());

    mList.addAll(mListOriginal);
    mAdapter = new ArrayAdapter<>(mActivity, android.R.layout.select_dialog_singlechoice, mList);
  }

  @Override public void initView(View view, Bundle savedInstanceState) {
    super.initView(view, savedInstanceState);
    setTitleCustom("功能搜索");
    setListAdapter(mAdapter);
    //禁用下拉刷新
    getPullRefreshListView().setMode(PullToRefreshBase.Mode.DISABLED);
    showViewByType(SHOW_EMPTY_VIEW);//否则会显示loading.
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {

    inflater.inflate(R.menu.menu_search, menu);
    menu.add(Menu.NONE, MENU_ITEM_CLEAR, 0, getString(R.string.clearChoose))
        .setIcon(R.drawable.delete)
        .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
    MenuItem menuItem = menu.findItem(R.id.menu_item_search);//在菜单中找到对应控件的item
    final SearchView searchView = (SearchView) MenuItemCompat.getActionView(menuItem);
    //android.support.v7.widget.SearchView.setOnQueryTextListener
    if (searchView == null) return;
    menuItem.expandActionView();//自动打开搜索框,弹出软键盘.
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
      @Override public boolean onQueryTextSubmit(String query) {
        //ToastUtil.show("提交:"+query);
        return false;
      }

      @Override public boolean onQueryTextChange(String newText) {
        ArrayList<? extends FunctionObject> result = search(newText);
        mList.clear();
        mList.addAll(result);
        mAdapter.notifyDataSetChanged();
        return false;
      }
    });

    super.onCreateOptionsMenu(menu, inflater);
  }

  private ArrayList<FunctionObject> search(String newText) {
    ArrayList<FunctionObject> result = new ArrayList<>();
    if (MyStringUtil.isEmpty(newText)) {
      result = mListOriginal;
    } else {
      for (FunctionObject name : mListOriginal) {
        if (MyStringUtil.isContainsByFirstLetter(name.getTitle(), newText)) {
          result.add(name);
        }
      }
    }
    return result;
  }

  @Override public void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    if (position < mListview.getHeaderViewsCount()) return;
    FunctionObject select = mAdapter.getItem(position - l.getHeaderViewsCount());
    if (select == null) return;
    select(select);
  }

  private void select(FunctionObject select) {
    if (select == null) return;
    String title = select.getTitle();
    int code = select.getCode();
    SharedPreferencesUtil.setString(mActivity, SharedPreferencesUtil.KEY_LAUNCH_NAME, title);
    SharedPreferencesUtil.setInt(mActivity, SharedPreferencesUtil.KEY_LAUNCH_CODE, code);
    Intent i = new Intent();
    BaseParams<ZTreeNode> params = new BaseParams<>();
    params.setTitle(title);
    i.putExtra(BaseFragment.EXTRA_PARAMS, params);
    mActivity.setResult(Activity.RESULT_OK, i);
    mActivity.finish();
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == MENU_ITEM_CLEAR) {
      select(new FunctionObject());
    }
    return super.onOptionsItemSelected(item);
  }
}
